<?php

namespace Biz\Resources\Site;

use Biz\Dictionaries\Paper\PaperStatusDict;
use Biz\Models\User\User;
use Lumen\Http\Resources\Json\Resource;
use Biz\Constants\Site\SitePaperConstants;

/**
 * SitePaperResource.
 *
 * @license [http://www.85do.com] [杭州永奥网络科技有限公司]
 * @copyright Copyright (c) 2018-2026 Hangzhou Yongao Technology Co., Ltd. All rights reserved.
 */
class SitePaperResource extends Resource
{
    /**
     * Transform the resource into an array.
     *
     * @param \Illuminate\Http\Request $request
     *
     * @return array
     */
    public function toArray($request)
    {
        /** @var \Biz\Models\User\User $user */
        //$user = $request->user();

        return [
            'uid'             => $this->uid,
            'number'          => $this->number,
            'status'          => $this->status,
            'status_name'     => PaperStatusDict::get($this->status),
            'created_at'      => $this->created_at->getTimestamp(),
            $this->mergeWhen($this->relationLoaded('theme'), [
                'theme' => $this->getSitePaperThemeResource(),
            ]),
            $this->mergeWhen($this->relationLoaded('full'), [
                'full' => $this->getSitePaperFullResource(),
            ]),
            $this->mergeWhen($this->relationLoaded('authors'), [
                'authors' => $this->getSitePaperAuthorResourceCollection(),
            ]),
            $this->mergeWhen($this->relationLoaded('abstract'), [
                'abstract' => $this->getSitePaperAbstractResource(),
            ]),
            $this->mergeWhen($this->relationLoaded('abstract'), [
                'translations' => $this->getSitePaperTranslationsResourceCollection(),
            ]),
        ];
    }


    /**
     * Get resource.
     *
     * @return SitePaperFullResource|null
     */
    public function getSitePaperThemeResource()
    {
        if ($this->relationLoaded('theme')) {

            if ($this->theme_id === SitePaperConstants::THEME_DEFAULT) {
                return SitePaperConstants::THEME_NONE;
            } else {
                return new SitePaperThemeResource($this->theme);
            }
        }

        return null;
    }

//    /**
//     * Get status name.
//     *
//     * @param User $user
//     *
//     * @return string
//     */
//    public function getStatusName(User $user): string
//    {
//        if ($user->isClient()) {
//            return SitePaperConstants::$ClientStatusNames[$this->status];
//        } elseif ($user->isMember()) {
//            return SitePaperConstants::$MemberStatusNames[$this->status];
//        }
//
//        return '';
//    }

    /**
     * Get resource.
     *
     * @return SitePaperAbstractResource|null
     */
    public function getSitePaperAbstractResource()
    {
        if ($this->relationLoaded('abstract')) {
            return new SitePaperAbstractResource($this->abstract);
        }

        return null;
    }

    /**
     * Get resource.
     *
     * @return SitePaperFullResource|null
     */
    public function getSitePaperFullResource()
    {
        if ($this->relationLoaded('full')) {
            return new SitePaperFullResource($this->full);
        }

        return null;
    }

    /**
     * Get app resource.
     *
     * @return SitePaperTranslationsResourceCollection|null
     */
    public function getSitePaperAuthorResourceCollection()
    {
        if ($this->relationLoaded('authors')) {
            return new SitePaperAuthorResourceCollection($this->authors);
        }

        return null;
    }

    /**
     * Get app resource.
     *
     * @return SitePaperTranslationsResourceCollection|null
     */
    public function getSitePaperTranslationsResourceCollection()
    {
        if ($this->relationLoaded('translations')) {
            return new SitePaperTranslationsResourceCollection($this->translations);
        }

        return null;
    }
}
